<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use think\Controller;
use think\facade\Session;
use think\Request;
use think\captcha\Captcha;

class Index extends controller
{
    protected $middleware = [
        'CheckAdmin' => [
            'except' => ['login', 'loginPost', 'captcha']
        ]
    ];

    public function index()
    {
        return $this->fetch();
    }

    public function main()
    {
        return $this->fetch();
    }

    public function indexData()
    {
        $data = array();
        $total_sql = Db('user')
            ->field('count(*) as total,DATE(create_time) as today')
            ->group('today')
            ->select(false);
        $user_total = Db()
            ->table('(' . $total_sql . ') a')
            ->where('today', 'BETWEEN', [date('Y-m-d', strtotime('-30 day')), date('Y-m-d')])
            ->select();
        for ($date = strtotime(date('Y-m-d', strtotime('-30 day'))); $date <= strtotime(date('Y-m-d')); $date += 24 * 60 * 60) {
            $a = 0;
            foreach ($user_total as $val) {
                if (date('Y-m-d', $date) == $val['today']) {
                    $data['yaxis'][] = $val['total'];
                    $a = 1;
                }
            }
            if ($a == 0) {
                $data['yaxis'][] = 0;
            }
            $data['xaxis'][] = date('Y-m-d', $date);
        }
        return $data;
    }

    public function logout()
    {
        Session::clear();
        $this->redirect('index/login');
    }

    public function login(Request $request)
    {
        if ($request->isPost()) {
            $username = $request->post('username', '', 'trim');
            $password = $request->post('password', '', 'trim');
            $captcha_code = $request->post('captcha_code', '', 'trim');
            $captcha = new Captcha();
            if (!$captcha->check($captcha_code)) {
                return ['code' => 1, 'msg' => '验证码错误'];
            }
            $admin = Admin::where('username', $username)->find();
            if (!$admin) {
                return ['code' => 1, 'msg' => '用户不存在'];
            }
            if (!$admin->checkPassword($password)) {
                return ['code' => 1, 'msg' => '密码错误'];
            }
            if ($admin->status == 0) {
                return ['code' => 1, 'msg' => '用户已禁用'];
            }
            Session::set('admin_id', $admin->admin_id);
            Session::set('nickname', $admin->nickname);
            Session::set('type', $admin->type);
            Session::set('lastloginip', $admin->last_login_ip);
            Session::set('lastlogintime', $admin->last_login_time);
            $admin->last_login_ip = $request->ip();
            $admin->last_login_time = date('Y-m-d H:i:s');
            $admin->save();
            return ['code' => 0, 'msg' => '登陆成功'];
        } else {
            return $this->fetch();
        }
    }

    public function resetPassword(Request $request)
    {
        if ($request->isPost()) {
            $old_password = $request->post('old_password', '', 'trim');
            $new_password = $request->post('new_password', '', 'trim');
            $res_password = $request->post('res_password', '', 'trim');
            $admin = Admin::where('admin_id', Session::get('admin_id'))->find();
            if (!$admin->checkPassword($old_password)) {
                return ['code' => 1, 'msg' => '旧密码错误'];
            }
            if ($new_password != $res_password) {
                return ['code' => 1, 'msg' => '两次输入密码不一致'];
            }
            if ($new_password == '') {
                return ['code' => 1, 'msg' => '密码不能为空'];
            }
            if ($admin->setPassword($new_password)) {
                Session::clear();
                return ['code' => 0, 'msg' => '修改成功'];
            } else {
                return ['code' => 1, 'msg' => '修改失败'];
            }
        } else {
            return $this->fetch();
        }
    }

    public function navs(Request $request)
    {
        $navs = [
            'member' => [
                [
                    "title" => "后台首页",
                    "href" => url('index/main'),
                    "spread" => true
                ],
                [
                    "title" => "会员列表",
                    "href" => url('user/lists'),
                    "spread" => false
                ],
                [
                    "title" => "管理员管理",
                    "href" => url('admin/lists'),
                    "spread" => false
                ]
            ],
            'content' => [
                [
                    "title" => "后台首页",
                    "href" => url('index/main'),
                    "spread" => true
                ],
                [
                    "title" => "课程管理",
                    "href" => url('course/lists'),
                    "spread" => false
                ],
                [
                    "title" => "套餐管理",
                    "href" => url('product/lists'),
                    "spread" => false
                ],
                [
                    "title" => "往期精彩",
                    "href" => url('article/lists'),
                    "spread" => false
                ],
                [
                    "title" => "大咖学员",
                    "href" => url('student/lists'),
                    "spread" => false
                ],
                [
                    "title" => "专家团队",
                    "href" => url('teacher/lists'),
                    "spread" => false
                ],
                [
                    "title" => "案例管理",
                    "href" => url('cases/lists'),
                    "spread" => false
                ]
            ],
            'order' => [
                [
                    "title" => "后台首页",
                    "href" => url('index/main'),
                    "spread" => true
                ],
                [
                    "title" => "课程订单",
                    "href" => url('order/lists'),
                    "spread" => false
                ],
                [
                    "title" => "产品订单",
                    "href" => url('product_order/lists'),
                    "spread" => false
                ]
            ],
            'system' => [
                [
                    "title" => "后台首页",
                    "href" => url('index/main'),
                    "spread" => true
                ],
                [
                    "title" => "分类管理",
                    "href" => url('category/lists'),
                    "spread" => false
                ],
                [
                    "title" => "勋章管理",
                    "href" => url('medal/lists'),
                    "spread" => false
                ],
                [
                    "title" => "轮播图管理",
                    "href" => url('system/banner'),
                    "spread" => false
                ],
                [
                    "title" => "留言管理",
                    "href" => url('system/guestBook'),
                    "spread" => false
                ],
                [
                    "title" => "系统设置",
                    "href" => url('system/config'),
                    "spread" => false
                ]
            ]
        ];
        $active = $request->get('active', '', 'trim');
        if(session('type') != 1){
            unset($navs['member'][2]);
        }
        return $navs[$active];
    }

    public function captcha()
    {
        $captcha = new Captcha(['fontSize' => 20, 'length' => 4, 'fontttf' => '5.ttf', 'useCurve' => false]);
        return $captcha->entry();
    }
}
